package com.qianjiangcloud.store.util;

import android.content.Context;
import android.graphics.Point;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.qianjiangcloud.store.Run;
import com.qianjiangcloud.store.activity.AgentActivity;
import com.qianjiangcloud.store.ui.FlowView;
import com.qianjiangcloud.store.ui.MarqueeView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import com.qianjiangcloud.store.R;
import com.qianjiangcloud.store.libary.cache.MyImageLoader;

/**
 * 垂直新闻公告栏.
 */
public class NewsAdsUtils {
    private static ArrayList<JSONObject> mNewsAdsArray = null;
    private static Point mScreenSize = null;
    private static LinearLayout adsFrameLayout = null;
    private static View vi = null;
    private static FlowView mTopAdsView;
    private static Context context;
    private static MarqueeView marqueeView;
    private static ImageView icon_news;

    public static View getNewsAdsViews(Context mContext) {

        context = mContext;
        //创建布局
        mNewsAdsArray = new ArrayList<JSONObject>();
        WindowManager windowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        mScreenSize = Run.getScreenSize(windowManager);
        adsFrameLayout = new LinearLayout(mContext);
        LinearLayout.LayoutParams adsFrameLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        adsFrameLayout.setLayoutParams(adsFrameLayoutParams);
        adsFrameLayout.setOrientation(LinearLayout.VERTICAL);
        vi = LayoutInflater.from(mContext).inflate(R.layout.news_ads_items, null);
      //  setUpview(json_str,mContext,vi,img_url);
        adsFrameLayout.addView(LayoutInflater.from(context).inflate(R.layout.item_line, null));
        adsFrameLayout.addView(vi);
        return adsFrameLayout;
    }

    public static  void setUpview(String json_str, Context mContext, View view, String img_url) {
        icon_news = (ImageView) view.findViewById(R.id.icon_news);
        marqueeView= (MarqueeView) view.findViewById(R.id.marqueeView);
        marqueeView.setTag(json_str);
        marqueeView.setOnClickListener(mNewsAdViewClickListener);
        MyImageLoader.displayDefaultImage(img_url, icon_news);
        parseNewsAdsJson(json_str, mContext);
    }

    private static void parseNewsAdsJson(String json_str, Context mContext) {
        ArrayList<JSONObject> mNewsAdsArray = new ArrayList<JSONObject>();
        try {
            JSONObject all = new JSONObject(json_str);
            if (Run.checkRequestJson(mContext, all)) {
                JSONObject infoObject = all.getJSONObject("data");
                JSONArray child = infoObject.optJSONArray("info");
                if (child != null && child.length() > 0) {
                    mNewsAdsArray.clear();
                    for (int i = 0; i < child.length(); i++) {
                        JSONObject topJson = child.getJSONObject(i);
                        mNewsAdsArray.add(topJson);
                    }
                    if (mNewsAdsArray.size() > 0) {
                        reloadMainView(false, mNewsAdsArray);
                    } else {
                        reloadMainView(true, mNewsAdsArray);
                    }
                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void reloadMainView(boolean isOutDate, ArrayList<JSONObject> mNewsAdsArray) throws JSONException {
        if (!isOutDate) {
            marqueeView.startWithList(mNewsAdsArray);
        } else {
            vi.setVisibility(View.GONE);
        }
    }

    public static View.OnClickListener mNewsAdViewClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (null != v.getTag()) {
                JSONObject data = (JSONObject) v.getTag();
                String link_url = data.optString("link_url");
                context.startActivity(AgentActivity.intentForFragment(context,
                        AgentActivity.FRAGMENT_ARTICLE_READER).putExtra(
                        "com.shopex.westore.EXTRA_URL", data.optString("link_url")).putExtra("title",data.optString("news_title")));
               // Toast.makeText(context, link_url, Toast.LENGTH_SHORT).show();
            }

        }
    };


}
